﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Tools;
using Pixysoft.Framework.Noebe;
using Pixysoft.SQL;
using Pixysoft.Framework.Verifications;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Environment;
using System.Data;

namespace Pixysoft.Weblications.Sango.Dao
{
    class UsrSectionDao
    {
        public static Entities.Usr_Section CreateSection(Entities.Sys_Section section, Entities.Usr_Profile profile,
            double geolocationx, double geolocationy)
        {
            Entities.Usr_Section orm = Entities.Local.Usr_Section;
            orm.Primarycode = PkHelper.PrimaryCode();
            orm.Sectionserialcode = section.Sectionserialcode;
            orm.Sectiontypecode = section.Sectiontypecode;
            orm.Usercode = profile.Usercode;
            orm.Nickname = profile.Nickname;
            orm.Joindate = GlobalTimer.Instance.ToTimeSequence();
            orm.Geoloactionx = geolocationx;
            orm.Geolocationy = geolocationy;
            orm.Nationtypecode = section.Nationtypecode;
            orm.Scaletypecode = SectionScaleTypecodeCollection.SCALE_01;
            return orm;
        }

        public static Entities.Usr_Section GetSection(string usercode)
        {
            IEntityNoebeCommand command = SettingHelper.GetNoebe().EntityNoebeCommand;
            command.SQL = SqlCreator.SelectAll().From(Schemas.LOCAL.USR_SECTION)
            .Where(Schemas.LOCAL.USR_SECTION.USERCODE).Equal("USERCODE", true).GetSQL();

            command.Parameters.Add("USERCODE", usercode);
            List<Entities.Usr_Section> orms = command.ExecuteReader<Entities.Usr_Section>();
            if (orms.Count == 0)
                throw Exceptions.VerificationFailedException(usercode);
            return orms[0];
        }

        /// <summary>
        /// USERCODE
        /// NICKNAME
        /// USERLEVEL
        /// AVATARTYPECODE
        /// VIPLEVEL
        /// FAMETITLENAME
        /// </summary>
        /// <param name="usercode"></param>
        /// <param name="pageIndex"></param>
        /// <param name="sectionserialcode"></param>
        /// <param name="sectiontypecode"></param>
        /// <param name="geox"></param>
        /// <param name="geoy"></param>
        /// <returns></returns>
        public static DataTable GetSectionProfilesExclude(string usercode, long pageIndex, long sectionserialcode, long sectiontypecode, double geox, double geoy)
        {
            INoebeCommand command = SettingHelper.GetNoebe().NoebeCommand;
            command.SQL = SqlCreator.Select(
                Schemas.LOCAL.USR_PROFILE.USERCODE,
                Schemas.LOCAL.USR_PROFILE.NICKNAME,
                Schemas.LOCAL.USR_PROFILE.USERLEVEL,
                Schemas.LOCAL.USR_PROFILE.AVATARTYPECODE,
                Schemas.LOCAL.USR_PROFILE.VIPLEVEL,
                Schemas.LOCAL.USR_PROPERTY.FAMETITLENAME).From(Schemas.LOCAL.USR_PROFILE)
                .InnerJoin(Schemas.LOCAL.USR_PROPERTY).On(Schemas.LOCAL.USR_PROFILE.USERCODE).Equal(Schemas.LOCAL.USR_PROPERTY.USERCODE)
                .InnerJoin(Schemas.LOCAL.USR_SECTION).On(Schemas.LOCAL.USR_PROFILE.USERCODE).Equal(Schemas.LOCAL.USR_SECTION.USERCODE)
                .Where(Schemas.LOCAL.USR_SECTION.SECTIONSERIALCODE).Equal("SECTIONSERIALCODE", true)
                .And(Schemas.LOCAL.USR_SECTION.SCALETYPECODE).Equal("SCALETYPECODE", true)
                //.And(Schemas.LOCAL.USR_PROFILE.USERCODE).NotEqual("USERCODE", true)//是否排除自己
                .GetSQL() + string.Format(" LIMIT {0},{1}", pageIndex, SectionFormular.section_pagesize);

            command.Parameters.Add("USERCODE", usercode);
            command.Parameters.Add("SCALETYPECODE", sectiontypecode);
            command.Parameters.Add("SECTIONSERIALCODE", sectionserialcode);

            return command.ExecuteReader();
        }

        internal static bool needUpgradeSection(Pixysoft.Weblications.Sango.Entities.Usr_Profile profile)
        {
            //当主城等级达到一定规模，可以迁移到更级别的区域

            return false;
        }

        internal static void upgradeSection(Pixysoft.Weblications.Sango.Entities.Usr_Section section)
        {
            // do nothing
        }

        public static void moveSection(Entities.Sys_Section sysSection, Entities.Usr_Section orm,
            double geolocationx, double geolocationy)
        {
            orm.Primarycode = PkHelper.PrimaryCode();
            orm.Sectionserialcode = sysSection.Sectionserialcode;
            orm.Sectiontypecode = sysSection.Sectiontypecode;

            orm.Joindate = GlobalTimer.Instance.ToTimeSequence();
            orm.Geoloactionx = geolocationx;
            orm.Geolocationy = geolocationy;
            orm.Nationtypecode = sysSection.Nationtypecode;

        }
    }
}
